/*
 TSortList
*/
template<class T>
class TSortList : public TList<T>
{
    typedef TList<T> Base;
public:
    class ISortComparer;
private:
    ref<ISortComparer> mSortComparer;
public:
    inline TSortList(ref<ISortComparer> enm)
    {
        mSortComparer = enm;
    }

    inline T Add(T& inst)
    {
        if(Base::Length() == 0)
        {
            return Base::Add(inst);
        }
        else
        {
            for(typename Base::Iterator it = Base::Begin(), end = Base::End(); it!=end; ++it)
            {
                if(mSortComparer->Sort(it->GetObject(), inst))
                {
                    return Insert(*it, inst);
                }
            }
        }
        //throw error
        return NullPtr;
    }
};

/*
 SortList
*/
template<class T>
class SortList : public ref<TSortList<T> >
{
    typedef ref<TSortList<T> > Base;
public:
    typedef typename TSortList<T>::ListItem ListItem;
    typedef typename TSortList<T>::Iterator Iterator;
public:
    inline SortList() : Base() {}
    inline SortList(ref<TSortList<T> > copy) : Base(copy) {} // for Cast
};


/*
 ISortComparer
*/

template<class T>
class TSortList<T>::ISortComparer : public TObject
{
public:
    virtual bool Sort(T item1, T item2) = 0;
};
